package com.example.health_life.service;

import com.example.health_life.dao.Feedback;
import com.example.health_life.dao.FeedbackRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;

@Service
public class FeedbackServiceImpl implements  FeedbackService{

    @Autowired
    private FeedbackRepository feedbackRepository;

    @Override
    public List<Feedback> getFeedbackList() {
        return feedbackRepository.findAll();
    }

    @Override
    public Feedback getFeedbackById(Integer id) {
        return feedbackRepository.findById(id).get();
    }

    @Override
    public List<Feedback> getFeedbackByUserId(Integer userId) {
        return feedbackRepository.findByUserId(userId);
    }

    @Override
    public List<Feedback> getFeedbackByAbout(Integer aboutId) {
        if(aboutId == null){
            return null;
        }else if(aboutId == 0){
            return feedbackRepository.findByAbout((byte)0);
        }else if(aboutId == 1){
            return feedbackRepository.findByAbout((byte)1);
        } else if (aboutId == 2) {
            return feedbackRepository.findByAbout((byte)2);
        }else if (aboutId == 3){
            return feedbackRepository.findByAbout((byte)3);
        }else if(aboutId == 4){
            return feedbackRepository.findByAbout((byte)4);
        }else {
            return null;
        }

    }

    @Override
    public Integer addFeedback(Integer userId, String content, Byte about, String phone) {
        Feedback feedback = new Feedback();
        feedback.setUserId(userId);
        if(content != null){
            feedback.setContent(content);
        }
        feedback.setAbout(about);
        if(phone != null){
            feedback.setPhone(phone);
        }
        feedback.setFeedbackTime(Instant.now());
        return feedbackRepository.save(feedback).getId();
    }
}
